package cn.com.pagoda.ideplugin.listener;

import cn.com.pagoda.ideplugin.ui.*;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;

/**
 * @author tianmingxing <mx.tian@qq.com> on 2023/05/19
 */
public class PagodaFrameListener implements ActionListener, WindowListener {

    private final PagodaFrame frame;

    public PagodaFrameListener(PagodaFrame frame) {
        this.frame = frame;
    }

    @Override
    public void windowOpened(WindowEvent e) {

    }

    /**
     * 窗口即将关闭时触发的事件
     *
     * @param e the event to be processed
     */
    @Override
    public void windowClosing(WindowEvent e) {
        frame.setVisible(false);
    }

    @Override
    public void windowClosed(WindowEvent e) {
    }

    @Override
    public void windowIconified(WindowEvent e) {
    }

    @Override
    public void windowDeiconified(WindowEvent e) {
    }

    @Override
    public void windowActivated(WindowEvent e) {
    }

    @Override
    public void windowDeactivated(WindowEvent e) {
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == frame.getJsonEditorBtn()) {
            buttonEnable(frame.getJsonEditorBtn());
            frame.getCardLayout().show(frame.getMainPanel(), JsonEditorPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getYamlPropertiesConverterPanelBtn()) {
            buttonEnable(frame.getYamlPropertiesConverterPanelBtn());
            frame.getCardLayout().show(frame.getMainPanel(), YamlPropertiesConverterPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getTextComparisonBtn()) {
            buttonEnable(frame.getTextComparisonBtn());
            frame.getCardLayout().show(frame.getMainPanel(), TextComparisonPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getEncodeAndDecodeBtn()) {
            buttonEnable(frame.getEncodeAndDecodeBtn());
            frame.getCardLayout().show(frame.getMainPanel(), TranscoderPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getTimeConversionBtn()) {
            buttonEnable(frame.getTimeConversionBtn());
            frame.getCardLayout().show(frame.getMainPanel(), TimeConversionPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getPwdGenBtn()) {
            buttonEnable(frame.getPwdGenBtn());
            frame.getCardLayout().show(frame.getMainPanel(), PwdGenPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getRegexpBtn()) {
            buttonEnable(frame.getRegexpBtn());
            frame.getCardLayout().show(frame.getMainPanel(), RegexpPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getCrontabGenBtn()) {
            buttonEnable(frame.getCrontabGenBtn());
            frame.getCardLayout().show(frame.getMainPanel(), CrontabGenPanel.class.getSimpleName());
        } else if (e.getSource() == frame.getExcelConversionBtn()) {
            buttonEnable(frame.getExcelConversionBtn());
            frame.getCardLayout().show(frame.getMainPanel(), ExcelConversionPanel.class.getSimpleName());
        }
    }

    private void buttonEnable(JButton btn) {
        frame.getJsonEditorBtn().setEnabled(!frame.getJsonEditorBtn().equals(btn));
        frame.getYamlPropertiesConverterPanelBtn().setEnabled(!frame.getYamlPropertiesConverterPanelBtn().equals(btn));
        frame.getTimeConversionBtn().setEnabled(!frame.getTimeConversionBtn().equals(btn));
        frame.getExcelConversionBtn().setEnabled(!frame.getExcelConversionBtn().equals(btn));
        frame.getPwdGenBtn().setEnabled(!frame.getPwdGenBtn().equals(btn));
        frame.getEncodeAndDecodeBtn().setEnabled(!frame.getEncodeAndDecodeBtn().equals(btn));
//        frame.getTextComparisonBtn().setEnabled(!frame.getTextComparisonBtn().equals(btn));
//        frame.getRegexpBtn().setEnabled(!frame.getRegexpBtn().equals(btn));
//        frame.getCrontabGenBtn().setEnabled(!frame.getCrontabGenBtn().equals(btn));
    }
}
